package com.fubang.rxjavalearning.mapOperator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.fubang.rxjavalearning.R;

import java.util.ArrayList;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.observables.GroupedObservable;

public class MapActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        //map操作符
        Observable.just(1, 2, 3, 4)
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return "This is " + integer;
                    }
                }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        });

        //scan操作符
        Observable.just("1", "2", "3", "4", "5")
                .scan(new Func2<String, String, String>() {
                    @Override
                    public String call(String string, String string1) {
                        return string + string1;
                    }
                }).subscribe(new Action1<String>() {
            @Override
            public void call(String integer) {
                System.out.println(integer);
            }
        });

        //groupBy操作符
        ArrayList<House> houses = new ArrayList<>();
        houses.add(new House("中粮·海景壹号", "中粮海景壹号新出大平层！总价4500W起"));
        houses.add(new House("竹园新村", "满五唯一，黄金地段"));
        houses.add(new House("中粮·海景壹号", "毗邻汤臣一品"));
        houses.add(new House("竹园新村", "顶层户型，两室一厅"));
        houses.add(new House("中粮·海景壹号", "南北通透，豪华五房"));

        Observable<GroupedObservable<String, House>> groupedObservableObservable = Observable.from(houses).groupBy(new Func1<House, String>() {
            @Override
            public String call(House house) {
                return house.getName();
            }
        });

        Observable.concat(groupedObservableObservable)
                .subscribe(new Action1<House>() {
                    @Override
                    public void call(House house) {
                        System.out.println("小区:" + house.getName() + "描述:" + house.getDesc());
                    }
                });

    }
}
